import { Component, OnInit } from '@angular/core';
import { FcsService } from '../service/fcs.service';
import { Router } from "@angular/router";
import { NavHeaderService } from '@global/layout/default/nav-header/nav-header.service';
import { CommonService } from '../service/common.service';
import {KeycloakService} from 'keycloak-iop/keycloak.service';

@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.less']
})
export class OverviewComponent implements OnInit {
  overviewData;
  functionPercent: number;
  invokePercent: number;
  computerPercent: number;
  invoHelp: string = '每个账号每月可以免费执行100万次函数调用，超出免费额度的调用次数会收取费用。每个自然月最后一天 23:59:59后，已用计数会自动清零，并重新统计账户内免费额度的使用情况。上个自然月未使用完的免费额度，不能累计到当前自然月。';
  compHelp: string = '每个账户每月可以免费使用40万GB*S的资源，超出免费额度的资源用量会收取费用。每个自然月最后一天23:59:59后，已用计数会自动清零，并重新统计账号内免费额度的使用情况。上个自然月未使用完的免费额度，不能累计到当前自然月。';
  functionNum;
  currentRegion = {
    regionCode: '',
    regionName: ''
  };
  lastDate;
  isAdmin = true; // 是否是主账号
  constructor(
    private fcsService: FcsService,
    private router: Router,
    private navHeaderService: NavHeaderService,
    private commonService: CommonService,
    private keycloakService: KeycloakService,
  ) { }

  getOverviewInfo() {
    this.fcsService.getRegionMetrics().subscribe(
      data => {
        this.overviewData = data;
        // TODO: 应该使用去尾法计算
        // 优化调用次数显示
        this.overviewData["formatInvokeCount"] = this.overviewData.invokeCount;
        // 过亿
        if (this.overviewData["formatInvokeCount"] >= 100000000) {
          let count = (this.overviewData["formatInvokeCount"] / 100000000).toFixed(2);
          this.overviewData["formatInvokeCount"] = count + "亿";
        }
        // 过十万 
        else if (this.overviewData["formatInvokeCount"] >= 100000) {
          let count = (this.overviewData["formatInvokeCount"] / 10000).toFixed(0);
          this.overviewData["formatInvokeCount"] = count + "万";
        }
        // 过万
        else if (this.overviewData["formatInvokeCount"] >= 10000) {
          let count = (this.overviewData["formatInvokeCount"] / 10000).toFixed(2);
          this.overviewData["formatInvokeCount"] = count + "万";
        }
        // 优化资源使用量显示
        this.overviewData["formatComputePowert"] = this.overviewData.computePower;
        // 过亿
        if (this.overviewData["formatComputePowert"] >= 100000000) {
          let count = (this.overviewData["formatComputePowert"] / 100000000).toFixed(2);
          this.overviewData["formatComputePowert"] = count + "亿";
        }
        // 过十万
        else if (this.overviewData["formatComputePowert"] >= 100000) {
          let count = (this.overviewData["formatComputePowert"] / 10000).toFixed(0);
          this.overviewData["formatComputePowert"] = count + "万";
        }
        // 过万
        else if (this.overviewData["formatComputePowert"] >= 10000) {
          let count = (this.overviewData["formatComputePowert"] / 10000).toFixed(2);
          this.overviewData["formatComputePowert"] = count + "万";
        }
        // 少于一万
        else if (this.overviewData["formatComputePowert"] < 10000) {
          let count = (this.overviewData["formatComputePowert"]).toFixed(0);
          this.overviewData["formatComputePowert"] = count;
        }

        let num: number = parseFloat(data['functionCount']) / 200;
        this.functionPercent = Number((num * 100).toFixed(2));
        let invoNum: number = parseFloat(data['invokeCount']) / 1000000;
        this.invokePercent = Number((invoNum * 100).toFixed(2));
        let computerNum: number = parseFloat(data['computePower']) / 400000;
        this.computerPercent = Number((computerNum * 100).toFixed(2));
      }
    )
  }

  getOverviewFunctionNum() {
    this.fcsService.getFunctionNum().subscribe(
      data => {
        this.functionNum = data;
      }
    )
  }

  getCurrentRegion() {
    this.commonService.getRegions().subscribe(res => {
      if (res && res['data']) {
        let regions = res['data'];
        let regionId = this.navHeaderService.getCurrentRegion();
        let regionName = this.commonService.getRegionName(regions, regionId);
        this.currentRegion['regionCode'] = regionId;
        this.currentRegion['regionName'] = regionName;
      }
    })
  }

  ngOnInit() {
    this.isAdmin = this.keycloakService.getUsername().indexOf('.') === -1;
    this.getOverviewInfo();
    this.getCurrentRegion();
    this.getLastDate();
    this.getOverviewFunctionNum();
  }

  createFunction() {
    this.router.navigate(['/fcs-manage/function/create'], { queryParams: { regionCode: this.currentRegion.regionCode, regionName: this.currentRegion.regionName } });
  }

  createApp() {
    this.router.navigate(['/application']);
  }

  createTrriger() {
    this.router.navigate(['/fcs-manage/trigger']);
  }

  createSelfweb() {
    this.router.navigate(['/domain']);
  }

  getLastDate() {
    let ld = this.commonService.getLastDate();
    this.lastDate = ld + " 23:59:59";
  }

  introduction() {
    window.open("/document/fcs/1-service-introduction.html");
  }

  percentFormat(percent) {
    if (percent >= 100) {
      return "已用尽"
    }
    return percent + "%"
  }
}
